﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
	<meta charset="utf-8"/>
    <title>黄冈师范学院选课系统登录</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/login.css"/>
	
</head>
<body>
    <div class="main">
    	<div class="mainin">
        	<img src="${pageContext.request.contextPath}/images/logo.png" class="log"/>

            <div class="mainin1" action="login" method="get">
            	<ul>
                    <li>
                        <p class="identity">角色:</p>
                        <select class="identity">
                            <option value="学生">学生</option>
                            <option value="教师">教师</option>
                            <option value="管理员">管理员</option>
                        </select>
                    </li>
                	<li><span>用户名：</span><input type="text"  name="aid"  value="2015211040113" id="loginName" placeholder="编号" class="SearchKeyword"/></li>
                    <li><span>密码：</span><input type="password" name="apsw" value="123456" id="password" placeholder="密码" class="SearchKeyword2"/></li>

                    <li style="padding: 0;"><input type="text" name="checkcode" class="verify_label" id="verify_input"/><a id="verify_link" href="javascript:reloadImg()"><img id="verify" src="${pageContext.request.contextPath}/admin/verify?ddd" alt="验证码"/></a><label id="verify_label">看不清?换一张</label></li>
<%--
                    <img id="ddd" onclick="document.getElementById('ddd').src='verify?'+new Date().getTime();" src="verify?ddd" >
--%>
                    <li><button id="login" class="tijiao" value="登录">登录</button><button class="tijiao" id="clear">清空</button></li>
                </ul>
            </div>
        </div>
    </div>
    <img src="${pageContext.request.contextPath}/images/loading.gif" id="loading" style=" display:none;position:absolute;" />

</div>
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>
<script>
    (function () {
        $('#verify_label').click(reloadImg);
    })();
    function reloadImg() {
        $('#verify').attr('src','${pageContext.request.contextPath}/admin/verify?'+new Date().getTime());
    }
	(function () {

	    function sendReq(checkcode,aid,apsw) {

            var msg = {
                checkcode:checkcode
            };
            var url="/admin/login";
            var id = $('.identity option:selected').attr('value');
            console.log('id'+id);
            if(id=='学生'){
                msg.sid=aid;
                msg.spsw=apsw;
                url="/student/login"
            }
            else if (id=='教师'){
                msg.tid=aid;
                msg.tpsw=apsw;
                url="/teacher/login"
            }
            else {
                msg.aid=aid;
                msg.apsw=apsw;
                url="/admin/login"
            }
            console.log(msg);
            $.ajax({
                url: '${pageContext.request.contextPath}'+url,
                data: msg,
                success: function (data) {
                    if (data) {
                        if (data == 'success') {
                            window.location.href = "${pageContext.request.contextPath}/index";
                        }
                        else {
                            $('#verify').attr('src','${pageContext.request.contextPath}/admin/verify?'+new Date().getTime());
                            alert('编号或密码输入错误!');
                        }
                    }
                }
            });

        }
		var aidReg = /^[0-9]{1,20}$/;
		var checkcodeReg = /^[0-9]{4}/;
		$('#login').click(function () {
			var aid = $('#loginName').val();
            var checkcode = $('#verify_input').val();
            console.log(checkcode);
            if (aidReg.test(aid)) {
                var apsw = $('#password').val();
                if (apsw != '') {
                    if (checkcodeReg.test(checkcode)) {
                        sendReq(checkcode,aid,apsw);
                    }
                    else {
                        alert('请输入合法的验证码!')
                    }
                }
                else {

                    alert('请输入密码!')
                }
            }
            else {
                alert('请输入合法的编号!');
            }
        });



        $(document).keyup(function (e) {
            var keyCode = 0;
            var e = e || window.event;
            keyCode = e.keyCode || e.which || e.charCode;
            var aid = $('#loginName').val();
            var checkcode = $('#verify_input').val();
            if (keyCode == '13') {

                if (aidReg.test(aid)) {
                    var apsw = $('#password').val();
                    if (apsw != '') {
                        if (checkcodeReg.test(checkcode)) {
                            sendReq(checkcode,aid,apsw);
                        }
                        else {
                            alert('请输入合法的验证码!')
                        }
                    }
                    else {

                        alert('请输入密码!')
                    }

                }
                else {
                    alert('请输入合法的编号!');
                }
            }

        });
    })()

</script>
</body>
</html>
